package com.hz.web.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hz.web.entity.CkHtmlEntity;
import com.hz.web.service.IckHtmlService;
import com.hz.web.util.Constant;
import com.hz.web.util.ParmsEntity;
import com.hz.web.util.UtilMethods;

@Controller
@RequestMapping("ck")
public class CkController{

    @Resource
    private IckHtmlService service;

    /**
     * 根据富文本类型查询富文本信息(前端首页展示)
     * 
     * @param entity
     * @return
     */
    @RequestMapping("queryCkHtmlQian")
    @ResponseBody
    public Map<String, Object> queryCkHtmlQian(ParmsEntity entity) {
        Map<String, Object> result = new HashMap<String, Object>();// 结果返回map
        Map<String, Object> parmsMap = new HashMap<String, Object>();// 参数map
        int ckType = entity.getCkType();
        int page = entity.getPage();
        int rows = entity.getRows();
        String state = entity.getState();
        String ckTypeList = entity.getCkTypeList();
        if (ckType == 0) {
            if (ckTypeList == null || "".equals(ckTypeList.trim())) {
                return result;
            }
        }
        List<Integer> stateList = null;
        if ("zcfg".equals(ckTypeList)) {// 政策法规
            stateList = new ArrayList<Integer>();
            stateList.add(10);
            stateList.add(21);
            stateList.add(22);
            stateList.add(23);
            parmsMap.put("list", stateList);
        } else if ("xyyj".equals(ckTypeList)) { // 信用研究 11 12 13
            stateList = new ArrayList<Integer>();
            stateList.add(11);
            stateList.add(12);
            stateList.add(13);
            parmsMap.put("list", stateList);
        } else if ("dxal".equals(ckTypeList)) {// 典型案例 26 27
            stateList = new ArrayList<Integer>();
            stateList.add(26);
            stateList.add(27);
            parmsMap.put("list", stateList);
        } else if ("tpxx".equals(ckTypeList)) {// 图片滚动信息
            stateList = new ArrayList<Integer>();
            stateList.add(6);
            stateList.add(7);
            stateList.add(8);
            stateList.add(9);
            parmsMap.put("list", stateList);
            parmsMap.put("imgpd", "cx");
        } else if ("tzgg".equals(ckTypeList)) { // 通知公告 32 33 34
            stateList = new ArrayList<Integer>();
            stateList.add(32);
            stateList.add(33);
            stateList.add(34);
            parmsMap.put("list", stateList);
        }
        if (state != null && !"".equals(state.trim())) {
            parmsMap.put("state", state.trim());
        }
        parmsMap.put("ckType", ckType);// 富文本类型 判断是哪个功能的富文本信息
        if (page != 0 && rows != 0) {
            int index = (page - 1) * rows;
            int end = rows;
            parmsMap.put("index", index);
            parmsMap.put("end", end);
        }
        parmsMap.put("sort", "yes");
        // 根据参数map查询出的富文本编辑的结果集合
        List<CkHtmlEntity> list = new ArrayList<CkHtmlEntity>();
        list = service.queryCkHtml(parmsMap);
        result.put("rows", list);
        return result;
    }

    /**
     * 跳转到 不显示列表的 富文本页面
     * 
     * @return
     */
    @RequestMapping("toHtmlManage")
    public String toHtmlManage() {
        return "html_manage";
    }

    /**
     * 修改富文本信息
     * 
     * @param entity
     * @return
     */
    @RequestMapping("updateCkHtml")
    @ResponseBody
    public Map<String, Object> updateCkHtml(CkHtmlEntity entity) {
        String title = entity.getTitle();
        if ("CKEditorHtml".equals(title)) {
            entity.setCreateTime(new Date());
            entity.setState(Constant.CKSTATS1);// 默认是显示的
            entity.setId(0);// 用类型作为查询条件使用
        } else {
            entity.setCkType(0);// 用id作为查询条件使用 正常修改是不会修改 富文本类型的
        }
        Map<String, Object> result = new HashMap<String, Object>();// 结果返回map
        int row = -1;
       
        try {
            row = service.updateCkHtml(entity);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        result.put("row", row);
        return result;
    }

    /**
     * 向富文本表里插入信息
     * 
     * @param entity
     * @return
     */
    @RequestMapping("addCkHtml")
    @ResponseBody
    public Map<String, Object> addCkHtml(CkHtmlEntity entity) {
        Map<String, Object> result = new HashMap<String, Object>();// 结果返回map
        int row = -1;
        try {
            entity.setState(Constant.CKSTATS1);// 默认是显示的
            row = service.addCkHtml(entity);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        result.put("row", row);
        return result;
    }

    /**
     * 根据富文本id 或者 富文本类型 ckType 查询富文本信息
     */
    @RequestMapping("queryHtmlById")
    @ResponseBody
    public Map<String, Object> queryHtmlById(CkHtmlEntity ckHtml) {
        Map<String, Object> result = new HashMap<String, Object>();// 结果返回map
        Map<String, Object> parmsMap = new HashMap<String, Object>();// 参数map
        int id = ckHtml.getId();
        int ckType = ckHtml.getCkType();
        if (id != 0) {
            parmsMap.put("id", id);
        }
        if (ckType != 0) {
            parmsMap.put("ckType", ckType);
        }
        String state = "";
        state = ckHtml.getState();
        parmsMap.put("state", state);

        List<CkHtmlEntity> html = new ArrayList<CkHtmlEntity>();
        CkHtmlEntity entity = new CkHtmlEntity();
        html = service.queryCkHtml(parmsMap);
        if (html.size() > 0) {
            entity = html.get(0);
            String htmlName = "";
            htmlName = UtilMethods.getNameByHtmlType(entity.getCkType());
            entity.setTypeName(htmlName);
        }
        result.put("entity", entity);
        return result;
    }

    /**
     * 跳转到富文本页面
     * 
     * @return
     */
    @RequestMapping("toAddUpdatehtml")
    public String toAddUpdatehtml() {
        return "addUpdateHtml";
    }

    /**
     * 跳转到富文本页面
     * 
     * @return
     */
    @RequestMapping("toCkhtml")
    public String toCkhtml() {
        return "ckhtml";
    }

    /**
     * 根据富文本类型查询富文本信息
     * 
     * @param entity
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("queryCkHtml")
    @ResponseBody
    public Map<String, Object> queryCkHtml(ParmsEntity entity, String sort, String inputVal,String listCktype) throws UnsupportedEncodingException {
        if (inputVal != null && !"".equals(inputVal.trim())) {
            // inputVal = URLDecoder.decode(URLDecoder.decode(inputVal));
            inputVal = URLDecoder.decode(URLDecoder.decode(inputVal, "utf-8"), "utf-8");
        }
        Map<String, Object> result = new HashMap<String, Object>();// 结果返回map
        Map<String, Object> parmsMap = new HashMap<String, Object>();// 参数map
        int ckType = entity.getCkType();
        int page = entity.getPage();
        int rows = entity.getRows();
        String state = entity.getState();
        /*if(ckType==0){
        	return result;
        }*/
        if (inputVal != null && !"".equals(inputVal.trim())) {
            parmsMap.put("inputVal", "%" + inputVal.trim() + "%");
        }
        if (state != null && !"".equals(state.trim())) {
            parmsMap.put("state", state.trim());
        }
        if (listCktype != null && !"".equals(listCktype.trim())) {
        	List<String> list = new ArrayList<String>();
        	String[] listSz =listCktype.split(",");
        	for (String string : listSz) {
        		list.add(string);
			}
            parmsMap.put("list", list);
        }
        
        parmsMap.put("ckType", ckType);// 富文本类型 判断是哪个功能的富文本信息
        parmsMap.put("sort", sort);
        if (page != 0 && rows != 0) {
            int index = (page - 1) * rows;
            int end = rows;
            parmsMap.put("index", index);
            parmsMap.put("end", end);
            int count = 0;
            count = service.queryCkHtmlCount(parmsMap);
            result.put("total", count);
        }

        // 根据参数map查询出的富文本编辑的结果集合
        List<CkHtmlEntity> list = new ArrayList<CkHtmlEntity>();
        list = service.queryCkHtml(parmsMap);
        String htmlName = "";
        htmlName = UtilMethods.getNameByHtmlType(entity.getCkType());
        for (CkHtmlEntity ckHtml : list) {
            ckHtml.setTypeName(htmlName);
        }
        result.put("htmlName", htmlName);
        result.put("rows", list);

        return result;
    }

}
